package com.jhws.business.feedback.controller;

import com.alibaba.fastjson.JSON;
import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.feedback.bo.FeedbackConstant;
import com.jhws.business.feedback.bo.FeedbackInfo;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.business.feedback.service.FeedbackService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/feedback")
public class FeedbackCtrl {

    @Resource
    private FeedbackService feedbackService;
    @Resource
    private ITblUserDimService iUserService;
    @Resource
    private OSSClientService ossClientService;

    /**
     * 保存反馈信息
     */
    @RequestMapping("save.x")
    @ResponseBody
    public JsonRespData saveFeedback(FeedbackInfo feedbackInfo) throws Exception {
        TblUserDimBo userInfo = iUserService.currentUser();
        feedbackInfo.setState(FeedbackConstant.FEEDBACK_STATE_NOT_HANDLED);
        feedbackInfo.setUser(userInfo.getLogName());
        feedbackInfo.setUserName(userInfo.getUserName());
        feedbackInfo.setCommCode(userInfo.getCommCode());

        List<String> urls = new ArrayList<>();
        List<String> tempUrls = JSON.parseArray(feedbackInfo.getUrls(), String.class);
        for (String url : tempUrls) {
            String fileName = FilenameUtils.getName(url);
            String destKey = "cms/file/" + fileName;
            ossClientService.gerneralCopyFile(url, destKey);
            urls.add(destKey);
        }
        feedbackInfo.setType(FeedbackConstant.FEED_TYPE_WEB_USER);
        feedbackInfo.setUrls(JSON.toJSONString(urls));
        feedbackService.saveFeedback(feedbackInfo);
        return new JsonRespData("", 1, "成功");
    }

    /**
     * 保存处理结果
     */
    @RequestMapping("saveTreatmentResult.x")
    @ResponseBody
    public JsonRespData saveTreatmentResult(String id, String treatmentResult) {
        feedbackService.updateFeedback("set " +
                "treatment_result = '" + treatmentResult + "',state = '" + FeedbackConstant.FEEDBACK_STATE_IS_HANDLED + "'", " where id = '" + id + "'");
        return new JsonRespData("", 1, "成功");
    }

    /**
     * 查询反馈信息
     */
    @RequestMapping("query.x")
    @ResponseBody
    public Object queryFeedback(@RequestParam Map param) {
        return feedbackService.findFeedbackListPage(param);
    }

    /**
     * 查询当前用户手机号
     */
    @RequestMapping("queryUserPhone.x")
    @ResponseBody
    public JsonRespData queryUserPhone() {
        TblUserDimBo userInfo = iUserService.currentUser();
        return new JsonRespData("", 1, userInfo.getTelNum());
    }
}
